/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import helpers.DateOfBirth;
import helpers.Response;
import java.io.Serializable;
import java.sql.*;
import java.util.Calendar;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
import models.User;

/**
 *
 * @author Rasztemberg
 */

@Named(value="registerUserBean")
@RequestScoped
public class RegisterUser implements Serializable{

    private User user; 
    private DateOfBirth dateOfBirth;
    private boolean errorCreatingUser = false;
    
    public RegisterUser(){
        user = new User();
        dateOfBirth = new DateOfBirth();
    }
    
   // HELPER FUNCTIONS ------------------------------------------------------------
   
   /*
    * create a java calendar instance
    * get a java.util.Date from the calendar instance.
    * this date will represent the current instant, or "now".
    * a java current time (now) instance
    */
   public Timestamp getTimeStampNow(){
       Calendar calendar = Calendar.getInstance();  
       java.util.Date now = calendar.getTime();
       Timestamp t = new Timestamp(now.getTime());
       return t;
   }
    
    // ACTIONS -------------------------------------------------------------------
    public String registerUser() {
        user.setRegistrationDate(getTimeStampNow());
        int res = user.insert();
        if(res >= 1){                 
            errorCreatingUser = false;
            return Response.SUCCESS;
        }
        else {
            errorCreatingUser = true;
            return Response.FAIL;
        }
    }  
    
    
    // GETTERS -------------------------------------------------------------------
    public User getUser() {
        return user;
    }

    public boolean isErrorCreatingUser() {
        return errorCreatingUser;
    }

    public void setErrorCreatingUser(boolean errorCreatingUser) {
        this.errorCreatingUser = errorCreatingUser;
    }

    public DateOfBirth getDateOfBirth() {
        return dateOfBirth;
    }
    
    // SETTERS ------------------------------------------------------------------
    public void setUser(User user) {
        this.user = user;
    }

    public void setDateOfBirth(DateOfBirth dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }    
}
